package com.kaibes.web.util;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.kaibes.core.base.util.ListUtils;
import com.kaibes.core.base.util.StringUtils;
import com.kaibes.web.util.model.AgentEngine;
import com.kaibes.web.util.model.UserAgent;

public class UserAgentUtils {
    
    public static boolean isComeFromWeb(HttpServletRequest request) {
        return request.getHeader("User-Agent").startsWith("Mozilla");
    }
    
    public static String getUserAgentStr(HttpServletRequest request) {
        return request.getHeader("User-Agent");
    }
    
    public static UserAgent getUserAgent(HttpServletRequest request) {
        String userAgentStr = request.getHeader("User-Agent");
        if (StringUtils.isEmpty(userAgentStr)) {
            return null;
        }
        return getUserAgent(userAgentStr);
    }

    public static UserAgent getUserAgent(String userAgentStr) {
        
        byte[] bytes = userAgentStr.getBytes();
        List<String> tempList = new ArrayList<>();
        
        UserAgent agent = new UserAgent();
        agent.setUserAgentStr(userAgentStr);
        
        byte[] temp = new byte[64];
        int index = 0;
        byte[] temp2 = new byte[256];
        int index2 = 0;
        
        boolean flag = false;
        AgentEngine engine = new AgentEngine();
        for (int i = 0; i < bytes.length; i++) {
            if (index > temp.length) {
                break;
            }
            
            if (flag) {
                if (index2 == -1) {
                    if (bytes[i] == ')') {
                        i++;
                        flag = false;
                    }
                    continue;
                }
                
                switch (bytes[i]) {
                case ';':
                    tempList.add(get(temp, index));
                    index = 0;
                    temp2[index2++] = bytes[i];
                    break;
                    
                case ' ':
                    temp2[index2++] = bytes[i];
                    break;
                    
                case ')':
                    tempList.add(get(temp, index));
                    agent.setSystemArr(ListUtils.toArray(tempList));
                    index = 0;
                    
                    agent.setSystemStr(get(temp2, index2));
                    index2 = -1;
                    
                    i++;
                    flag = false;
                    break;
                    
                default:
                    temp[index++] = bytes[i];
                    temp2[index2++] = bytes[i];
                    break;
                }
            } else {
                switch (bytes[i]) {
                case '/':
                    engine.setEngineName(get(temp, index));
                    index = 0;
                    break;
                    
                case ' ':
                    if (engine.getEngineName() == null) {
                        engine.setEngineName(get(temp, index));
                    } else {
                        engine.setEngineVersion(get(temp, index));
                    }
                    agent.getEngineList().add(engine);
                    engine = new AgentEngine();
                    index = 0;
                    break;
                    
                case '(':
                    flag = true;
                    break;
                    
                default:
                    temp[index++] = bytes[i];
                    break;
                }
            }
        }
        
        return agent;
    }
    
    private static String get(byte[] temp, int index) {
        byte[] target = new byte[index];
        System.arraycopy(temp, 0, target, 0, index);
        return new String(target);
    }
}
